$(function() {

    var cuf;
    var acuf = $('[id$=cohorte_unidad_formacion_id]').val();
    $.get(
            '/admin/ajax/cohoUdefRel',
            function(data) {
                cuf = data;
                $('.select_udef').each(function() {
                    var name = $(this).attr('name');
                    var index = parseInt(name.match(/\d{1,2}/));
                    var udef_id = parseInt($('[id$=' + index + '_unidad_formacion_id]').val());
                    var coho_id = parseInt($('[id$=' + index + '_cohorte_id]').val());
                    $('[id$=' + index + '_cohorte_unidad_formacion_id]').val(cuf.rel[udef_id][coho_id]);
                });

                if ($('[id$=0_unidad_formacion_id]').val() == 0) {
                    var elem = $('[id$=0_unidad_formacion_id]');
                    var new_items = '';
                    for (key in cuf.udef) {
                        new_items = new_items + '<option value="' + key + '">' + cuf.udef[key] + '</option>\n';
                    }
                    elem.html(new_items);
                    elem.val(1);
                    RelCohoUdef(elem.get(0));
                }

                if (acuf != '') {
                    for (var x in cuf.rel) {
                        for (var y in cuf.rel[x]) {
                            if (cuf.rel[x][y] == acuf) {
                                $('[id$=0_unidad_formacion_id] option[value="' + x + '"]').attr('selected', true);
                                $('[id$=0_cohorte_id] option[value="' + y + '"]').attr('selected', true);
                                $('[id$=0_cohorte_id]').val(y);
                            }
                        }
                    }
                }
            }, 'json');
    //var cuf = $.parseJSON(coho_udef);

    function RelCohoUdef(elem) {
        var name = $(elem).attr('name');
        var index = parseInt(name.match(/\d{1,2}/));
        var udef_id = parseInt($('[id$=' + index + '_unidad_formacion_id]').val());
        var coho_id = parseInt($('[id$=' + index + '_cohorte_id]').val());
        var new_items = '';
        for (key in cuf.coho[udef_id]) {
            new_items = new_items + '<option value="' + key + '">' + cuf.coho[udef_id][key] + '</option>\n';
        }
        $('[id$=' + index + '_cohorte_id]').html(new_items);
        $('[id$=' + index + '_cohorte_unidad_formacion_id]').val(cuf.rel[$('[id$=' + index + '_unidad_formacion_id]').val()][$('[id$=' + index + '_cohorte_id]').val()]);
    }

    $('.select_udef').on('change', function() {
        RelCohoUdef(this);
    });

    $('.select_coho').on('change', function() {
        var name = $(this).attr('name');
        var index = parseInt(name.match(/\d{1,2}/));
        var udef_id = parseInt($('[id$=' + index + '_unidad_formacion_id]').val());
        $('[id$=' + index + '_cohorte_unidad_formacion_id]').val(cuf.rel[$('[id$=' + index + '_unidad_formacion_id]').val()][$('[id$=' + index + '_cohorte_id]').val()]);
    });
});